#include "Common.h"
#include "BackBuffer.h"
#include "Render.h"

BackBuffer::BackBuffer()
	: mSurface(NULL)
	, mDepthStencile(NULL)
{
}

BackBuffer::~BackBuffer()
{
	SAFE_RELEASE(mSurface);
	SAFE_RELEASE(mDepthStencile);
}

void BackBuffer::Init()
{
	SAFE_RELEASE(mSurface);

	LPDIRECT3DDEVICE9 device = Render::Instance().getDevice();
	VERIFY_D3D(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &mSurface));

	// just get the back buffer's depth stencile.
	if (mDepthStencile == NULL)
		VERIFY_D3D(device->GetDepthStencilSurface(&mDepthStencile));

	D3DSURFACE_DESC surfaceDesc;
	VERIFY_D3D(mSurface->GetDesc(&surfaceDesc));
	
	mWidth = surfaceDesc.Width;
	mHeight = surfaceDesc.Height;
	mFormat = surfaceDesc.Format;
	mDepth = false;;
}

void BackBuffer::Attach()
{
	LPDIRECT3DDEVICE9 device = Render::Instance().getDevice();
	VERIFY_D3D(device->SetRenderTarget(0, mSurface));
	VERIFY_D3D(device->SetDepthStencilSurface(mDepthStencile));
}

void BackBuffer::Dettach()
{
	// do nothing...
}